<?php
namespace app\api\controller;

use think\Request;
use think\Session;
use think\controller\Rest;
use app\api\controller\Base;

/**
* 用户模块
*/
class User extends Base
{
	
	/**
	 *查询用户好友
	 *
	 * @param [int] $[id] [用户ID]
	 * @return [json] [用户好友]
	 * 
	 */
	public function friendList($id,$key = '',$ids = '')
	{
		$Base = new Base();

		if(!is_numeric($id))
		{
			return $res = $Base->json(400,'用户ID错误',array());
		}
		# 检查用户是否存在
		
		# 联合查询用户好友
		$friendList = db('users')
		->alias('u')
		->field('u.id,u.name')
		->join('social_contact s','u.id = s.user_id')
		->where('s.master_id',$id)
		->where('u.id','not in',$ids)
		->where('u.name|u.phone','like','%'.$key.'%')
		->select();	
		# 查询用户信息
		if(strpos($ids,$id)===false)
		{
			$friendList[] = db('users')->where('id',$id)->field('id,name')->find();
		}
		
		# 返回json数据
		return $res = $Base->json(200,'查询成功',$friendList);
	}

	/**
	 *用户注册
	 *
	 * @param [array] $[data] [用户注册信息]
	 * @return [json] [返回信息]
	 */
	public function register()
	{
		
	}

	/**
	 *用户登录
	 *
	 * @param [array] $[data] [用户登录信息]
	 * @return [json]
	 */
	public function login()
	{
		$data = input('post.');
		# 验证用户信息的完整
		if(!$data['phone'])
		{
			return Base::json(400,"请填写用户名");
		}
		if(!$data['password'])
		{
			return Base::json(400,"请填写密码");
		}
		# 查询用户是否存在
		$where = array(
			'phone' => $data['phone'],
			'password' => md5($data['password'])
		);
		$userInfo = db()->where($where)->find();
		if(!$userInfo)
		{
			return Base::json(400,"用户不存在");
		}
		unset($userInfo["password"]);
		session("User", $userInfo);
		return Base::json(200,"登录成功");
	}
}
?>